package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import computerPlayer.AgressivePlayer;
import computerPlayer.MinMaxPlayer;
import computerPlayer.Player;
import computerPlayer.RandomPlayer;

public class ComputerplayerPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel playerPanelPC = new JPanel(new GridLayout(2,1));
	private JPanel playerPanelHuman = new JPanel(new GridLayout(2,1));;
	private JTextField nameLabel = new JTextField("Name: ");
	private JTextField charLabel = new JTextField("Zeichen: ");
	private JComboBox playerListEAST;
	private String[] playerStrings = { "HumanPlayer", "MinMaxPlayer", "AgressivePlayer", "RandomPlayer" };
	private Border raisedBevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED);
	private JFrame view;
	private String choosenPlayer;

	public ComputerplayerPanel(JFrame view) {
		this.view = view;
		setBackground(view.getBackground());
		//Create the combo box, select item at index 4.
		//Indices start at 0, so 4 specifies the pig.
		playerListEAST = new JComboBox(playerStrings);
		playerListEAST.setSelectedIndex(2);
		choosenPlayer = playerStrings[2];
		playerListEAST.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
		        String playerArt = (String)cb.getSelectedItem();
		        changePanel(playerArt);
		        setChoosenPlayer(playerArt);
			}
		});
		setLayout(new BorderLayout());
		add(playerListEAST, BorderLayout.NORTH);
		setPreferredSize(new Dimension(250, 150));
		setBorder(raisedBevelBorder);
		validate();
		setVisible(true);
		view.repaint();
	}
	
	private void removeComponent(){
		Component [] comps = this.getComponents();
		for (int i = 0; i < comps.length; i++) {
			if(comps[i].equals(playerPanelHuman) || comps[i].equals(playerPanelPC)){
				remove(comps[i]);
			}
		}
	}
	
	private void initpcPanel(Player player){
		removeComponent();
		getNameLabel().setText("Name: " + player.getPlayerName());
		getCharLabel().setText("Zeichen: " + player.getPlayerChar());
		getNameLabel().setBorder(raisedBevelBorder);
		getCharLabel().setBorder(raisedBevelBorder);
		getNameLabel().setEditable(false);
		getCharLabel().setEditable(false);
		playerPanelPC.setBackground(view.getBackground());
		playerPanelPC.add(getNameLabel());
		playerPanelPC.add(getCharLabel());
		playerPanelPC.validate();
		add(playerPanelPC, BorderLayout.CENTER);
		this.validate();
		setVisible(true);
	}
	
	private void initHumanPanel(){
		removeComponent();
		getNameLabel().setText("Spielername");
		getCharLabel().setText("X");
		getNameLabel().setBorder(raisedBevelBorder);
		getCharLabel().setBorder(raisedBevelBorder);
		getNameLabel().setEditable(true);
		getCharLabel().setEditable(true);
		playerPanelHuman.setBackground(view.getBackground());
		playerPanelHuman.add(getNameLabel());
		playerPanelHuman.add(getCharLabel());
		add(playerPanelHuman, BorderLayout.CENTER);
		this.validate();
		setVisible(true);
	}
	
	public void changePanel(String player){
		if(player.equals("HumanPlayer")){
			initHumanPanel();
		}
		else if(player.equals(playerStrings[1])) {
			initpcPanel(new MinMaxPlayer(null));
		}
		else if(player.equals(playerStrings[2])) {
			initpcPanel(new AgressivePlayer(null,null));
		}
		else if(player.equals(playerStrings[3])) {
			initpcPanel(new RandomPlayer(null, null));
		}
		
	}

	public String getChoosenPlayer() {
		return choosenPlayer;
	}

	public void setChoosenPlayer(String choosenPlayer) {
		this.choosenPlayer = choosenPlayer;
	}

	public JTextField getNameLabel() {
		return nameLabel;
	}

	public void setNameLabel(JTextField nameLabel) {
		this.nameLabel = nameLabel;
	}

	public JTextField getCharLabel() {
		return charLabel;
	}

	public void setCharLabel(JTextField charLabel) {
		this.charLabel = charLabel;
	}
	
	
}
